﻿using abcLike.com;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MuaBanLike.com
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        private PopupWindow wnd = null;
        private DateTime startTime;

        string m_sUserWeb;
        string m_sPassWeb;
        public MainWindow()
        {
            InitializeComponent();          
            //initMenuContext();
            
            m_sUserWeb = Settings.Default.User;
            m_sPassWeb = Settings.Default.Pass;
           

            HideScriptErrors(MainBrowser, true);
            m_mainWeb = new Uri("https://abclike.com/");

            m_LikeFacebook = new Uri(m_mainWeb.ToString() + "p.php?p=facebook");
            m_FollowFacebook = new Uri(m_mainWeb.ToString() + "p.php?p=fb_subscribe");
            m_ShareFacebook = new Uri(m_mainWeb.ToString() + "p.php?p=fb_share");
            m_googlePlus = new Uri(m_mainWeb.ToString() + "p.php?p=google");
            m_trafficExchange = new Uri(m_mainWeb.ToString() + "p.php?p=surf");

        }

        private void onLoadCompleted(object sender, NavigationEventArgs e)
        {
            if(m_serviceActive == ServiceAction.LIKE)
            {
                onLikeFakceBook();
            }
            else if (m_serviceActive == ServiceAction.FOLLOW)
            {
                onFollowFakceBook();
            }
        }

        private void onNavigated(object sender, NavigationEventArgs e)
        {
            //Start detect to login
            if (e.Uri != null)
            {
                if (e.Uri.ToString() == "https://www.facebook.com/")
                {
                    //startThreadFacebook();
                }
                else if (e.Uri.ToString() == m_mainWeb.ToString())
                {
                    startLoginThread();
                }
            }
        }

        private void onLoaded(object sender, RoutedEventArgs e)
        {
            checkFacebookLogin();
        }

        private void LikeBtn_Click(object sender, RoutedEventArgs e)
        {
            onClosePopup();
            m_serviceActive = ServiceAction.LIKE;
            try
            {
                IntPtr pHandle = GetCurrentProcess();
                SetProcessWorkingSetSize(pHandle, -1, -1);
                MainBrowser.Source = m_LikeFacebook;                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void follow_Click(object sender, RoutedEventArgs e)
        {
            onClosePopup();
            m_serviceActive = ServiceAction.FOLLOW;
            try
            {
                IntPtr pHandle = GetCurrentProcess();
                SetProcessWorkingSetSize(pHandle, -1, -1);
                MainBrowser.Source = m_FollowFacebook;             
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void stopBtn_Click(object sender, RoutedEventArgs e)
        {
            //m_bFacebookLogin = true;
            try
            {
                IntPtr pHandle = GetCurrentProcess();
                SetProcessWorkingSetSize(pHandle, -1, -1);
                //If You Want to define a defualt URI then you can define like below...
                MainBrowser.Source = m_mainWeb;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //Fix issue close popup when main is closed
        private void onClosed(object sender, EventArgs e)
        {
            onClosePopup();
        }

       
    }
}
